﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
namespace Quality.spc
{
    public partial class splineImage : Form
    {
        DataTable dt;
        public splineImage()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            getdata frm = new getdata();

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEventdt += frm_TransfEventdt;
            frm.StartPosition = FormStartPosition.CenterParent;

            frm.Show();
        } 
        //事件处理方法
        void frm_TransfEventdt(DataTable value)
        {
            dataGridView1.DataSource = value;
            this.dt = value;
        }

       

        private void button1_Click(object sender, EventArgs e)
        {
            if (dt == null) {
                return;
            }
            chart1.Series.Clear();

            chart1.Series.Add("测量值");//添加 
            chart1.Series[0].ChartType = SeriesChartType.Spline;
            chart1.Series.Add("上限");//添加 
            chart1.Series[1].ChartType = SeriesChartType.Spline;
            chart1.Series.Add("下限");//添加 
            chart1.Series[2].ChartType = SeriesChartType.Spline;
            chart1.Series.Add("平均值");//添加 
            chart1.Series[3].ChartType = SeriesChartType.Spline;
            double[] v=new double[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                v[i] = Convert.ToDouble(dt.Rows[i][1]);
            }

           avg.Text= SixSigmaHelper.GetAverageValue(v).ToString();

           
             max.Text =dt.Compute("max(testvalue)", "true").ToString();
             min.Text=dt.Compute("min(testvalue)", "true").ToString();
             count.Text = dt.Compute("count(testvalue)", "true").ToString();

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                chart1.Series[0].Points.AddXY(i, dt.Rows[i][1]);
             
            }
            for (int i = 0; i <dt.Rows.Count; i++)
            {


                chart1.Series[3].Points.AddXY(i, Convert.ToDouble(avg.Text));
                chart1.Series[1].Points.AddXY(i, Convert.ToDouble(usl.Text));
                chart1.Series[2].Points.AddXY(i, Convert.ToDouble(lsl.Text));
            }
        }
    }
}
